home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / src / GLperf3.12-src.lha / GLperf / RastrPos.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-01  |  7.3 KB  |  207 lines

  1. /*
  2.  * (c) Copyright 1995, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED
  4.  * Permission to use, copy, modify, and distribute this software for
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *
  25.  * US Government Users Restricted Rights
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * Author: John Spitzer, SGI Applied Engineering
  36.  *
  37.  */
  38.  
  39. #include <math.h>
  40. #include "RastrPos.h"
  41. #include <malloc.h>
  42.  
  43. void new_RasterPos(RasterPosPtr this)
  44. {
  45.     new_Primitive((PrimitivePtr)this);
  46.     /* Set virtual functions */
  47.     this->SetState = RasterPos__SetState;
  48.     this->delete = delete_RasterPos;
  49. }
  50.  
  51. void delete_RasterPos(TestPtr thisTest)
  52. {
  53.     RasterPosPtr this = (RasterPosPtr)thisTest;
  54.  
  55.     delete_Primitive(thisTest);
  56. }
  57.  
  58. int RasterPos__SetState(TestPtr thisTest)
  59. {
  60.     RasterPosPtr this = (RasterPosPtr)thisTest;
  61.  
  62.     /* set parent state */
  63.     if (Primitive__SetState(thisTest) == -1) return -1;
  64.     Primitive__SetProjection((PrimitivePtr)this, this->rasterPosDim);
  65.  
  66.     /* set own state */
  67.  
  68.     return 0;
  69. }
  70.  
  71. void RasterPos__AddTraversalData(RasterPosPtr this)
  72. {
  73.     GLfloat x, y;
  74.     int i, j, k;
  75.     int rasterPosDataSize, traversalDataSize, dataSize;
  76.     int numDrawn = this->numDrawn;
  77.     GLfloat* newTraversalData;
  78.     GLfloat* newptr;
  79.     GLfloat* ptr = this->traversalData;
  80.     int rgba = this->environ.bufConfig.rgba;
  81.     int colorData = this->colorData;
  82.     int normalData = this->normalData;
  83.     int textureData = this->textureData;
  84.     const GLfloat colorFactor = 0.8;
  85.     GLfloat texFactorX, texFactorY, texFactorZ;
  86.     int windowDim = min(this->environ.windowWidth, this->environ.windowHeight);
  87.     GLdouble modelMatrix[16];
  88.     GLdouble projMatrix[16];
  89.     GLint viewport[4];
  90.     GLdouble xd, yd, zd;
  91.     GLdouble depthBits, epsilon;
  92.     GLdouble base, range, delta;
  93.  
  94.     int rampsize = rgba ? 0 : (1 << this->environ.bufConfig.indexSize);
  95.  
  96.     rasterPosDataSize = (colorData == PerRasterPos) ? ((rgba) ? this->colorDim : 1) : 0;
  97.     rasterPosDataSize += (textureData == PerRasterPos)
  98.                           ? ((this->texture==GL_TEXTURE_1D)
  99.                              ? 1 
  100.                              : ((this->texture==GL_TEXTURE_2D)
  101.                                 ? 2
  102.                                 : 3)) 
  103.                           : 0;
  104.     rasterPosDataSize += this->rasterPosDim;
  105.     dataSize = numDrawn * rasterPosDataSize;
  106.     newTraversalData = (GLfloat*)AlignMalloc(dataSize * sizeof(GLfloat), this->memAlignment);
  107.     newptr = newTraversalData;
  108.  
  109.     if (this->rasterPosDim == 3 && this->zOrder != Coplanar) {
  110.         glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
  111.         glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
  112.         glGetIntegerv(GL_VIEWPORT, viewport);
  113.         glGetDoublev(GL_DEPTH_BITS, &depthBits);
  114.     epsilon = pow(2.0, -depthBits);
  115.         mysrand(15000);
  116.  
  117.     switch (this->zOrder) {
  118.     case Random:
  119.         range = 1. - epsilon;
  120.         base = epsilon;
  121.         delta = 0.;
  122.         break;
  123.     case BackToFront:
  124.         range = (1. - epsilon) / (GLdouble)this->numDrawn;
  125.         base = 1. - range - epsilon;
  126.         delta = -range;
  127.         break;
  128.     case FrontToBack:
  129.         range = (1. - epsilon) / (GLdouble)this->numDrawn;
  130.         base = epsilon;
  131.         delta = range;
  132.         break;
  133.     }
  134.     }
  135.  
  136.     /* Figure out texture scaling factors given desired texture LOD */
  137.     if (textureData == PerRasterPos && this->environ.bufConfig.rgba) {
  138.         texFactorX = pow(2., this->texLOD) * 
  139.                      (float)this->environ.windowWidth / (float)this->texWidth;
  140.         texFactorY = pow(2., this->texLOD) * 
  141.                      (float)this->environ.windowHeight / (float)this->texHeight;
  142. #ifdef GL_EXT_texture3D
  143.         /* This will need to be fixed at some point... */
  144.         texFactorZ = pow(2., this->texLOD);
  145. #endif
  146.     }
  147.  
  148.     x = *ptr++;
  149.     y = *ptr++;
  150.     for (i=0; i<numDrawn; i++) {
  151.         if (colorData == PerRasterPos) {
  152.             if (rgba) {
  153.                 if (this->colorDim == 3) {
  154.                     AddColorRGBData(newptr, x, y, colorFactor);
  155.                     newptr += 3;
  156.                 } else {
  157.                     AddColorRGBAData(newptr, x, y, colorFactor);
  158.                     newptr += 4;
  159.                 }
  160.             } else {
  161.                 AddColorCIData(newptr, x, y, windowDim, rampsize);
  162.                 newptr += 1;
  163.             }
  164.         }
  165.         if (textureData == PerRasterPos && this->environ.bufConfig.rgba) {
  166.             if (this->texture == GL_TEXTURE_1D) {
  167.                 AddTexture1DData(newptr, x, y, texFactorX);
  168.                 newptr += 1;
  169.             } else if (this->texture == GL_TEXTURE_2D) {
  170.                 AddTexture2DData(newptr, x, y, texFactorX, texFactorY);
  171.                 newptr += 2;
  172.             } else { /* GL_TEXTURE_3D_EXT */
  173.                 AddTexture3DData(newptr, x, y, texFactorX, texFactorY, texFactorZ);
  174.                 newptr += 3;
  175.             }
  176.         }
  177.         if (this->rasterPosDim == 2) {
  178.             *newptr++ = x;
  179.             *newptr++ = y;
  180.         } else { /* rasterPosDim == 3 */
  181.             if (this->zOrder != Coplanar) {
  182.             GLdouble z = base + 
  183.                              delta * (GLdouble)i +
  184.                              range * (GLdouble)myrand()/(GLdouble)MY_RAND_MAX;
  185.                 gluUnProject((x+1.)/2.*(GLfloat)windowDim,
  186.                     (y+1.)/2.*(GLfloat)windowDim,
  187.                     z,
  188.                     modelMatrix,
  189.                     projMatrix,
  190.                     viewport,
  191.                     &xd, &yd, &zd);
  192.                 *newptr++ = (GLfloat)xd;
  193.                 *newptr++ = (GLfloat)yd;
  194.                 *newptr++ = (GLfloat)zd;
  195.             } else {
  196.                 *newptr++ = x;
  197.                 *newptr++ = y;
  198.                 *newptr++ = -1.;
  199.             }
  200.         }
  201.         x = *ptr++;
  202.         y = *ptr++;
  203.     }
  204.     AlignFree(this->traversalData);
  205.     this->traversalData = newTraversalData;
  206. }
  207.